<?php
declare (strict_types = 1);
namespace app\course\logic\base;

use app\common\exception\LogicException;
use app\course\model\base\SearchLog as SearchLogModel;

/**
 * 搜索接口
 */
class SearchLog extends SearchLogModel
{

    /**
     * 添加搜索记录
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-03
     */
    public function addLog($search)
    {
        $exist = $this->where('search', $search)->findOrEmpty();
        if ( $exist->isEmpty() ) {
            $this::create(['search'=>$search]);
        } else {
            $exist->times += 1;
            $exist->save();
        }
    }

    /**
     * 热门搜索
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-03
     */
    public function hotSearch()
    {
       $search = $this->where('times', '>', 1)->order('times','DESC')->limit(12)->select()->toArray();
       return array_column($search, 'search');
    }
}
